<?php

/**
 * 
 *
 */

class Tools{
	
	/**
	 * get user's ip 
	 * @return  string  user's ip
	 */
	public static function getClientIp()
	{
		
	
	}
	
	/**
	 * Cut string 
	 * @param string  Original string
	 * @return string   Be intercepted string
	 */
	public static function cutStr($str)
	{
		
		return $cStr;
	}
	
	/**
	 * Addslashes chars.
	 */
	 public static function saddslashes($string) 
	 {
		if(is_array($string))
		{
			foreach($string as $key => $val)
			{
				$string[$key] = self::saddslashes($val);
			}
		} 
		else 
		{
			$string = addslashes($string);
		}
		return $string;
	}

	/**
	 * Filter some specical chars.
	 */
	public static function  shtmlSpecialchars($string)
	{
		if(is_array($string))
		{
			foreach($string as $key => $val)
			{
				$string[$key] = self::shtmlspecialchars($val);
			}
		} 
		else 
		{
			$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
				str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
		}
		return $string;
	}

	// errno 0 无错误   1 key错误   2 无返回信息  3 缺少参数
	// result 返回数据
	public function returnJson($errno , $data = array())
	{
		header("Content-type: text/html; charset=utf-8");
		echo json_encode(array('errno'=>$errno , 'result'=>$data));
		die;
	}

	/**
	 * 
	 * 验证ajax请求
	 * @return boolean
	 */
	public function checkAjaxRequest()
	{
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
		{
			return true;
		}
		return false;
	}

/*
	public function objectToArray($obj)
	{
		$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
		foreach ($_arr as $key => $val){
			$val = (is_array($val) || is_object($val)) ? $this->objectToArray($val) : $val;
			$arr[$key] = $val;
		}
		return $arr;
	}
*/
	public function objectToArray($d) {  
	    if (is_object($d)) {  
	        // Gets the properties of the given object  
	        // with get_object_vars function  
	        $d = get_object_vars($d);  
	    }  
	  
	    if (is_array($d)) {  
	        /* 
	        * Return array converted to object 
	        * Using __FUNCTION__ (Magic constant) 
	        * for recursive call 
	        */  
	        return array_map(__FUNCTION__, $d);  
	    }  
	    else {  
	        // Return array  
	        return $d;  
	    }  
	}
	
	public static function boolImage($bool) {
		return '<img src="'.Yii::app()->baseUrl . '/global/img/icons/' . ($bool ? 'yes': 'no'). '.gif"  />';
	}
	
}